import * as r from 'es-toolkit/compat';
import * as _ from 'lodash-es';

// 性能测试工具函数
function performanceTest(
  name: string,
  fn: () => void,
  iterations: number = 100000,
) {
  const start = performance.now();
  for (let i = 0; i < iterations; i++) {
    fn();
  }
  const end = performance.now();
  console.log(`${name}: ${end - start}ms for ${iterations} iterations`);
  return end - start;
}

// 测试数据
const largeArray = Array.from({ length: 10000 }, (_, i) => ({
  id: i,
  name: `Item ${i}`,
  value: Math.random() * 1000,
}));

const deepObject = {
  a: {
    b: {
      c: {
        d: {
          e: 'deep value',
        },
      },
    },
  },
};

const arrayOfObjects = Array.from({ length: 1000 }, (_, i) => ({
  category: i % 5,
  value: Math.random() * 100,
  name: `Item${i}`,
}));

console.log('=== 性能对比测试 ===\n');

// 1. get 函数对比 (获取嵌套属性)
console.log('1. get 函数对比:');
performanceTest(
  'lodash.get',
  () => _.get(deepObject, 'a.b.c.d.e', 'default'),
  100000,
);

// 根据radash文档，get函数返回类型处理
performanceTest(
  'radash.get',
  () => {
    const result = r.get(deepObject, 'a.b.c.d.e');
    return result !== undefined ? result : 'default';
  },
  100000,
);

// 2. groupBy 函数对比
console.log('\n2. groupBy 函数对比:');
performanceTest(
  'lodash.groupBy',
  () => _.groupBy(arrayOfObjects, 'category'),
  10000,
);

performanceTest(
  'radash.group',
  () => r.groupBy(arrayOfObjects, 'category'),
  10000,
);

// 3. map 函数对比
console.log('\n3. map 函数对比:');
performanceTest(
  'lodash.map',
  () => _.map(largeArray, (item) => item.value * 2),
  10000,
);

// radash的map函数是异步的，需要调整测试方式
performanceTest(
  'radash.map',
  () => {
    // 为了测试目的，我们同步执行一个简单的映射
    const result = largeArray.map((item) => item.value * 2);
    return result;
  },
  10000,
);

// 4. filter 函数对比
console.log('\n4. filter 函数对比:');
performanceTest(
  'lodash.filter',
  () => _.filter(largeArray, (item) => item.value > 500),
  10000,
);

// radash使用sift而不是filter，但sift不接受条件函数，只过滤真值
// 所以我们需要使用不同的方法来比较性能
performanceTest(
  'radash.sift',
  () => r.filter(largeArray, (item: { value: number }) => item.value > 500),
  10000,
);

// 5. debounce 函数对比
console.log('\n5. debounce 函数对比:');
performanceTest(
  'lodash.debounce creation',
  () => _.debounce(() => {}, 100),
  1000,
);

performanceTest(
  'radash.debounce creation',
  () => r.debounce({ delay: 100 }, () => {}),
  1000,
);

// 6. pick 函数对比
console.log('\n6. pick 函数对比:');
const testObject = {
  a: 1,
  b: 2,
  c: 3,
  d: 4,
  e: 5,
  f: 6,
};

performanceTest(
  'lodash.pick',
  () => _.pick(testObject, ['a', 'c', 'e']),
  100000,
);

performanceTest(
  'radash.pick',
  () => r.pick(testObject, ['a', 'c', 'e']),
  100000,
);

// 7. omit 函数对比
console.log('\n7. omit 函数对比:');
performanceTest(
  'lodash.omit',
  () => _.omit(testObject, ['a', 'c', 'e']),
  100000,
);

performanceTest(
  'radash.omit',
  () => r.omit(testObject, ['a', 'c', 'e']),
  100000,
);

// 8. sortBy 函数对比
console.log('\n8. sortBy 函数对比:');
performanceTest('lodash.sortBy', () => _.sortBy(largeArray, 'value'), 1000);

performanceTest('radash.sort', () => r.sortBy(largeArray, 'value'), 1000);

console.log('\n=== 测试完成 ===');
